package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.beans.base.image;
import com.hdax.dm.beans.item.Item;
import com.hdax.dm.dto.recommendDto;
import com.hdax.dm.feign.imagefeign;
import com.hdax.dm.mappers.recommendMapper;
import com.hdax.dm.mapstucts.recommendDtoMapper;
import com.hdax.dm.service.recommendService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class recommendServiceImpl extends ServiceImpl<recommendMapper, Item> implements recommendService {

    @Autowired
    private imagefeign imagefeign;

    @Autowired
    private recommendDtoMapper recommendDtoMapper;

//    -----------------今日推荐---------------------
    @Override
    public CommonResponse<List<recommendDto>> recommend() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("isRecommend",1L);
        queryWrapper.eq("state",3L);
        queryWrapper.last("limit 6");

        List<Item> list = baseMapper.selectList(queryWrapper);

        List<recommendDto> dtos = recommendDtoMapper.toTargetList(list);
        for (recommendDto dto:dtos) {
            image img = imagefeign.image(dto.getId(),2L,1L);
            String imgUrl= img == null ? "":img.getImgUrl();
            dto.setImgUrl(imgUrl);
        }
        return ResponseUtil.returnSuccess(dtos);
    }

//    -----------------即将开售---------------------
    @Override
    public CommonResponse<List<recommendDto>> sell() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",2L);
        queryWrapper.last("limit 6");

        List<Item> list = baseMapper.selectList(queryWrapper);

        List<recommendDto> dtos = recommendDtoMapper.toTargetList(list);
        for (recommendDto dto:dtos) {
            image img = imagefeign.image(dto.getId(),2L,1L);
            String imgUrl= img == null ? "":img.getImgUrl();
            dto.setImgUrl(imgUrl);
        }
        return ResponseUtil.returnSuccess(dtos);
    }
}
